package com.xl.controller;

import com.xl.entity.EventInfo;
import com.xl.event.ConnectedEvent;
import com.xl.event.DisconnectEvent;
import com.xl.event.SendMsgEvent;
import com.xl.event.publisher.EventPublisherAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 请求controller发布事件，
 * 发布事件的方法：
 * 1 发布事件的时候可以使用自己构建的 EventPublisherAware.java对象（里面还是使用的ApplicationEventPublisher）进行发布
 * 2 直接注入ApplicationEventPublisher 对象，进行事件的发布 -- 这种反而更方便
 */
@RestController
public class EventController {

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    @RequestMapping("/connect")
    public void connect() {
        System.out.println("发布connect事件");
        EventInfo eventInfo = new EventInfo();
        eventInfo.setMsg("connect事件");
        ConnectedEvent connectedEvent = new ConnectedEvent(eventInfo);
        EventPublisherAware.publishEvetn(connectedEvent);
    }

    //使用注入的ApplicationEventPublisher对象进行发布
    @RequestMapping("/disConnect")
    public void disConnect() {
        System.out.println("发布disconnect事件");
        EventInfo eventInfo = new EventInfo();
        eventInfo.setMsg("disconnect事件");
        DisconnectEvent disconnectEvent = new DisconnectEvent(eventInfo);
        //使用注入的对象发布事件，而不是我自己定义的xxxaware
        applicationEventPublisher.publishEvent(disconnectEvent);
    }


    @RequestMapping("/sendMsg")
    public void sendMsg() {
        System.out.println("sendMsg");
        EventInfo eventInfo = new EventInfo();
        eventInfo.setMsg("sendMsg事件");
        SendMsgEvent sendMsgEvent = new SendMsgEvent(eventInfo);
        EventPublisherAware.publishEvetn(sendMsgEvent);
    }


}
